<template>
	<view class="flex flex-direction">
		<view class="add flex flex-direction">
			<view class="add-top flex align-center justify-between">
				<view class="flex align-center">
					<!--view>默认</view-->
					<text>收货人：{{defaultAddress.name}}</text>
				</view>
				<text>{{defaultAddress.mobile}}</text>
			</view>
			<view class="add-btn flex align-center">
				<image src="/static/b15.png"></image>
				<text>{{defaultAddress.area}}{{defaultAddress.address}}</text>
			</view>
			<image @click="toUpadta(defaultAddress)" src="/static/e6.png"></image>
		</view>
		<image mode="widthFix" style="width: 100%;" src="/static/b14.png"></image>
		<radio-group class="block" @change="RadioChange">
			<view class="list flex align-center flex-direction">
				<view v-for="(it,index) in addressList" class="foritem">
				<view class="item" @tap='changeaddress(it,index)'>
					<view class="item-top flex flex-direction" @tap='saveLocalAddress(it)'>
						<view class="flex align-center">
							<text>{{it.name}}</text>
							<text>{{it.mobile}}</text>
						</view>
						<text>{{it.area}}{{it.address}}</text>
					</view>
				</view>
				<view class="item-bto align-center justify-between">
					<view @tap="toUpadta(it)" class="align-center">
						<image src="/static/e6.png"></image>
						<text>编辑</text>
					</view>
				</view>
				</view>
				
				<!-- #ifdef MP-ALIPAY -->
				<view class="empty"><u-empty text="暂无地址" mode="list" v-if="addressList.length==0"></u-empty></view>
				<!-- #endif -->
				
			</view>
		</radio-group>
		<view style="100upx"></view>
		<view class="btn-con flex align-center justify-center">
			<view @click="toAddAddress" class="btn">新增地址</view>
		</view>
	</view>
</template>

<script>
	var api = require('@/components/api.js');
	export default {
		data() {
			return {
				addressList:[],
				user:'',
				defaultAddress:{name:'',mobile:'',area:'',address:''},
				isDefault:false,
				from:''
			}
		},
		onShow() {
			this.user = uni.getStorageSync('user')
			this.loadGetAddress()
		},
		onLoad(options) {
			console.log(options)
			this.user = uni.getStorageSync('user')
			this.loadGetAddress()
			if(options.from!=undefined){
				this.from=options.from
			}
		},
		methods: {
			loadGetAddress:function(){
				api.get({
					url:'User/GetAddress',
					data:{user_id:this.user.user_id},
					success:data=>{
						console.info('loadGetAddress'+JSON.stringify(data))
						if(data.ErrCode==0){
							this.addressList = data.Response
							for(var i=0;i<this.addressList.length;i++){
								if(this.addressList[i].is_default==2){
									this.defaultAddress = this.addressList[i]
									this.isDefault = true
									break
								}
							}
						}
					}
				});
			},
			changeaddress:function(it,index){
				it.is_default=2;
				this.addAddressEdit(this.addressList[index],1)
			},
			/**
			 * 设置默认地址
			 * **/
			addAddressEdit:function(item,redirect = 0){
				api.post({
					url: 'User/PostAddressEdit?address_id='+item.id+'&is_default=2&user_id='+this.user.user_id,
					data: {
						name:item.name,
						mobile:item.mobile,
						area:item.area,
						address:item.address,
						
					},
					success: data => {
						console.log(JSON.stringify(data));
						if (data.ErrCode == 0) {
							uni.showToast({
								title:'成功'
							})
							if(redirect == 1){
								uni.navigateBack()
							}else{
								uni.redirectTo({
									url:'/pages/receivingAddress/receivingAddress'
								})
							}
						}
						
					}
				});
			},
			saveLocalAddress:function(item){
				if(!this.isDefault){
					this.defaultAddress = item
				}
				console.log(JSON.stringify(item))
				uni.setStorageSync('isAddress',true)
				uni.setStorageSync('address',item)
			},
			toUpadta:function(item){
				console.log(this.from);
				var temp = JSON.stringify(item)
				uni.navigateTo({
					url:'/pages/updataAddress/updataAddress?from='+this.from+'&address='+temp,
				})
			},
			toAddAddress(){
				uni.navigateTo({
					url:"/pages/addAddress/addAddress"
				})
			},
			RadioChange(e) {
				console.log(e.detail.value)
				this.addAddressEdit(this.addressList[e.detail.value],1)
				
			},
		}
	}
</script>

<style lang="scss" scoped>
	.btn-con{
		position: fixed;
		bottom: 120rpx;
		width: 100%;
		height: 100upx;
		background-color: #fff;
		border-top: 1upx solid #ddd;
	}
	.btn{
			text-align: center;
			width: 560upx;
			line-height: 70upx;
				height: 70upx;
				background-color: $btn-bground;
				border-radius: 35upx;
				color: #fff;
				font-size: 28upx;
		}
	.add>image{
		width: 30upx;
			height: 30upx;
			position: absolute;
			right: 20upx;
			top: 70upx;
	}
	.add-btn{
		margin-top: 30upx;
	}
	.add-btn image{
			width: 24upx;
			height: 32upx;
	}
	.add-btn >text{
			font-size: 26upx;
			color: #000000;
			margin-left: 20upx;
		
	}
	.add-top{
		padding-left: 48upx;
		box-sizing: border-box;
		width: 630upx;
	}
	.add-top>text{
		font-size: 26upx;
			color: #000000;
	}
	.add-top>view>text{
			font-size: 28upx;
			color: #000000;
			margin-left: 14upx;
			font-weight: 500;
	}
	.add-top>view>view{
		width: 63upx;
			height: 34upx;
			background-color: $btn-bground;
			border-radius: 4upx;
			text-align: center;
			line-height: 34upx;
			color: #fff;
			font-size: 22upx;
	}
	.add {
		position: relative;
		width: 100%;
		background-color: #fff;
		padding: 30upx 20upx;
		box-sizing: border-box;
	}
	.item-top{
		padding: 24upx;
		box-sizing: border-box;
	}
	.item-bto image{
		width: 30upx;
		height: 30upx;
	}
	.item-bto text{
		font-size: 24upx;
			color: #000000;
			margin-left: 10upx;
	}
	.item-bto{
		height: 80upx;
		padding: 0 20upx;
		box-sizing: border-box;
		border-top: 1upx solid #ddd;
		float:right;
	}
	.item-top>view>text:nth-child(1){
		font-size: 26upx;
			color: #000000;
			width: 50%;
	}
	.item-top>view>text:nth-child(2){
			font-size: 26upx;
			color: #000000;
			width: 50%;
	}
	.item-top>text{
		font-size: 26upx;
			color: #666666;
			margin-top: 26upx;
	}
    .foritem{
		width:100%;
	}
	.item{
		width: 100%;
		background-color: #fff;
		margin-top: 20upx;
		z-index: 9999;
	}
	.editview{
		float:right;
		margin-right:10upx;
	}
	.editview image{
		width:30upx;
		height:30upx;
	}
</style>
